# Zhou-Horiuchi, IRT Pre-test

# Initial settings --------------------------------------------------------

dir.create("output/irt_pretest", recursive = TRUE, showWarnings = FALSE)
dir.create("figures/irt_pretest", recursive = TRUE, showWarnings = FALSE)
dir.create("logs", recursive = TRUE, showWarnings = FALSE)

source("functions/logging.R")
start_script_log("irt_pretest")

options(tidyverse.quiet = TRUE)
library(tidyverse)
library(estimatr)
library(ggthemes)
library(patchwork)
library(mirt)

# TracePlot ---------------------------------------------------------------

# I could not install and load the ggmirt package in the following way:
# install.packages("devtools")
# devtools::install_github("masurp/ggmirt")
# library(ggmirt)

# Thus, I manually saved the function from masurp's GitHub:
# https://github.com/masurp/ggmirt/blob/main/R/tracePlot.r
source("functions/tracePlot.r")

# Load Data ---------------------------------------------------------------

source("functions/read_Qualtrics.R")
data <- read_Qualtrics("data/irt_pretest_survey.csv")

# Clean Data ---------------------------------------------------------------------

data <- data %>% mutate(failed_check = (Q1.2 == "I do not agree to participate" | Q2.1 != "Every day,Never"))

# forward is when agree = high resentment

# Clean Data ---------------------------------------------------------------------

source("functions/scaling_pretest.R")
data <- data %>%
  select(ResponseId, failed_check, `Q1.2`:`Q34.1`) %>%
  filter(failed_check == FALSE) %>%
  rename(
    "s_side" = Q5.1,
    "s_israel" = Q5.2,
    "s_unsure" = Q5.3,
    "s_palestine" = Q5.4,
    "outcome" = Q6.1,
    "policy" = Q7.1,
    "j_acquaintances" = Q10.1,
    "j_friends" = Q11.1,
    "j_neighborhood" = Q12.1,
    "j_work" = Q13.1,
    "j_meal" = Q14.1,
    "j_conversations" = Q15.1,
    "j_pleasant" = Q16.1,
    "j_life" = Q17.1,
    "r_integrate" = Q19.1,
    "r_interests" = Q20.1,
    "r_violent" = Q21.1,
    "r_jihad" = Q22.1,
    "r_english" = Q23.1,
    "r_terrorist" = Q24.1,
    "r_speakout" = Q25.1
  ) %>%
  mutate(
    "j_acquaintances" = sapply(j_acquaintances, scale_friends),
    "j_friends" = sapply(j_friends, scale_friends),
    "j_neighborhood" = sapply(j_neighborhood, scale_describe),
    "j_work" = sapply(j_work, scale_describe),
    "j_meal" = sapply(j_meal, scale_often),
    "j_conversations" = sapply(j_conversations, scale_often),
    "j_pleasant" = sapply(j_pleasant, scale_often),
    "j_life" = sapply(j_life, scale_often),
    "v_israel" = case_when(
      s_side == "Israeli perspective" ~ 1,
      str_detect(s_unsure, "Israeli") ~ 1,
      TRUE ~ 0
    ),
    "v_palestine" = case_when(
      s_side == "Palestinian perspective" ~ 1,
      str_detect(s_unsure, "Palestinian") ~ 1,
      TRUE ~ 0
    ),
    "r_integrate" = scale_agree_rvrs(r_integrate),
    "r_interests" = scale_agree_fwd(r_interests),
    "r_violent" = scale_agree_fwd(r_violent),
    "r_jihad" = scale_agree_rvrs(r_jihad),
    "r_english" = scale_agree_fwd(r_english),
    "r_terrorist" = scale_agree_rvrs(r_terrorist),
    "r_speakout" = scale_agree_rvrs(r_speakout),
    "strong" = case_when(
      (str_starts(s_israel, "Strongly") | (str_starts(s_palestine, "Strongly"))) ~ 1,
      TRUE ~ 0
    )
  )

# Save Data ---------------------------------------------------------------

write_rds(data, str_c("output/irt_pretest/data_for_analysis.rds"))
cat("\n------------------------------------------------------------\n")
cat("Saved data: output/irt_pretest/data_for_analysis.rds\n")
cat("------------------------------------------------------------\n\n")


# IRT ---------------------------------------------------------------

contact_irt <- data %>%
  select(starts_with("j_")) %>%
  rename_with(~ gsub("^j_(\\w)", "\\U\\1", .x, perl = TRUE), starts_with("j_"))

fitGraded <- mirt(
  data = contact_irt,
  model = 1, # alternatively, we could also just specify model = 1 in this case
  itemtype = "graded"
)


# Testing fit of model
M2(fitGraded, type = "C2", calcNULL = FALSE)
itemfit(fitGraded)
head(personfit(fitGraded))

params <- coef(fitGraded, IRTpars = TRUE, simplify = TRUE)

score <- fscores(fitGraded)

# ICC_graded <-
tracePlot(fitGraded) +
  labs(color = "Contact Associated with Response", y = "Probability of Response", x = "Respondent Ability") +
  scale_color_manual(
    values = c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442"),
    labels = c(
      "Low", "Somewhat low", "Neutral",
      "Somewhat high", "High"
    )
  ) +
  theme(legend.position = "top")

ggsave("figures/irt_pretest/ICC.pdf", width = 8, height = 6)
cat("\n------------------------------------------------------------\n")
cat("Saved figure: figures/irt_pretest/ICC.pdf\n")
cat("------------------------------------------------------------\n\n")

end_script_log()
